<template>
	<view>
		<cu-custom bgColor="bg-white" isBack="true">
			<block slot="content">地址列表</block> 
		</cu-custom>
		<u-loading-page :loading="true" loading-text="loading..." v-if="showLoading"></u-loading-page>
		<view v-show="!showLoading">
			<view class="viewbox"></view>
			<view class="address_list" v-if="addressList.length>0">
				<view class="address_a" v-for="(item, index) in addressList" :key="index">
					<view class="address_a_d" :data-id="item.id" @tap="onSelect">
						<view class="address_a_d_a">{{ item.province + item.city + item.district}}</view>
						<view class="address_a_d_b">{{item.address }}</view>
						<view class="address_a_d_a">{{ item.contact }}<span>{{ item.telephone }}</span></view>
					</view>
					<view class="address_a_c">
						<view class="address_a_c_a" @click="setDefaultAddress(item.id)">
							<view class="address_a_c_a_a" :class="item.is_default==1?'address_a_c_a_aa':''" :style="'background-image:url('+moren+');'"></view>
							<view class="address_a_c_a_b">设为默认</view>
						</view>
						<view class="address_a_c_c" @click="showSurePop(item.id)" >删除</view>
						<view class="address_a_c_b" @click="editAddress(item.id)" >编辑</view>
					</view>
				</view>
			</view>
			<view v-else slot="empty" class="column-center" style="padding-top: 200rpx">
				<image class="img-null" src="/static/goods_null.png"></image>
				<text class="lighter">暂无收货地址</text>
			</view>
			<view class="address_add">
				<view class="address_add_a" @click="addAddress">新建收货地址</view>
			</view>
		</view>
	</view>
</template>
<script>
import request from '@/common/request.js';	
import { loadingType } from '@/utils/type';
export default {
	data() {
		return {
			showLoading:true,
			dz_dingwei:request.baseUrl_img+'/img/dz_dingwei.png',
			moren:request.baseUrl_img+'/img/moren.png',
			status: loadingType.LOADING,
			deleteSure: false,
			addressList:[],
			currentId: 0,
			uid:0,
			type:0
		}
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
	  this.type = options.type;
	},
	onShow() {
		this.uid=uni.getStorageSync('uid');
		this.getAddressListsFun();
	},
	methods: {
		setDefaultAddress(id){
			let paramsList = {
				uid:this.uid,
				id:id
			}
			let optsList = {
				url: 'ua-setDefault',
				method: 'post'
			};
			let _this=this;
			uni.showLoading({
				title: '设置中...',
				mask: true
			})
			request.httpTokenRequest(optsList, paramsList).then(res => {
				uni.showToast({
					title: res.data.msg,
					icon:'none',
					duration: 2000
				});
				uni.hideLoading();
				if(res.data.code == 200){
					if (this.type) {
					  uni.$emit('selectaddress', {
					    id
					  });
					  uni.navigateBack();
					}else{
						this.getAddressListsFun();
					}
				}
			});
		},
		onSelect(e) {
		  if (this.type) {
		    let {
		      id
		    } = e.currentTarget.dataset;
		    uni.$emit('selectaddress', {
		      id
		    });
		    uni.navigateBack();
		  }
		},
		delAddressFun() {
		  let id = this.currentId;
		  let paramsList = {
		  	uid:this.uid,
			id:id
		  }
		  let optsList = {
		  	url: 'ua-del',
		  	method: 'post'
		  };
		  let _this=this;
		  uni.showLoading({
		  	title: '删除中...',
		  	mask: true
		  })
		  request.httpTokenRequest(optsList, paramsList).then(res => {
			uni.showToast({
			    title: res.data.msg,
				icon:'none',
			    duration: 2000
			});
			uni.hideLoading();
		  	if(res.data.code == 200){
		  		this.deleteSure = false
		  		this.getAddressListsFun();
		  	}
		  });
		},
		showSurePop: function (e) {
		  let _this=this;
		  this.currentId = e;
		  uni.showModal({
		  	title: '提示',
		  	content: '您确认删除该地址吗？',
		  	success: function (res) {
		  		if (res.confirm) {
		  			_this.delAddressFun();
		  		}
		  	}
		  });
		},
		hidePop: function (e) {
		  this.deleteSure = false;
		},
		editAddress(id) {
		  uni.navigateTo({
		    url: `/pages/address_edit/address_edit?id=`+id
		  });
		},
		addAddress() {
		  uni.navigateTo({
		    url: '/pages/address_edit/address_edit'
		  });
		},
		getAddressListsFun() {
			let paramsList = {
				uid:this.uid
			}
			let optsList = {
				url: 'ua-lists',
				method: 'post'
			};
			let _this=this;
			request.httpTokenRequest(optsList, paramsList).then(res => {
				if(res.data.code == 200){
					_this.addressList = res.data.data;
					_this.status=loadingType.FINISHED;
				}else{
					_this.status=loadingType.EMPTY;
				}
				_this.showLoading=false;
			});
		}
	}
}
</script>

<style  lang="scss" scoped>
	.address_a{
		width: 702rpx;
		height:auto;
		background: #FFFFFF;
		border-radius: 20rpx;
		margin: auto;
		margin-top: 20rpx;
		overflow: hidden;
	}
	.address_a_d{
		overflow: hidden;
		padding: 40rpx 30rpx;
		border-bottom:1rpx solid #E5E5E5;
		width: 100%;
	}
	.address_a_d_a{
		font-size: 26rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #999999;
	}
	.address_a_d_a span{
		margin-left: 20rpx;
		font-size: 28rpx;
	}
	.address_a_d_b{
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: 800;
		color: #000000;
	}
	.address_a_c{
		width: 702rpx;
		height: 84rpx;
	}
	.address_a_c_a{
		float: left;
		margin-left: 30rpx;
		margin-top: 20rpx;
	}
	.address_a_c_a_a{
		width: 44rpx;
		height: 44rpx;
		background-color: #E5E5E5;
		border-radius: 50%;
		line-height: 44rpx;
		float: left;
		background-repeat: no-repeat;
		background-position: center;
		background-size: 21rpx 15rpx;
	}
	.address_a_c_a_aa{
		background-color:#19278f;
	}
	.address_a_c_a_b{
		font-size: 26rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #666666;
		margin-left: 15rpx;
		float: left;
	}
	.address_a_c_b{
		font-size: 26rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #666666;
		background-repeat: no-repeat;
		background-position:left;
		background-size: 27rpx 27rpx;
		padding-left: 40rpx;
		float:right;
		height: 84rpx;
		line-height: 84rpx;
		margin-right: 51rpx;
	}
	.address_a_c_c{
		font-size: 26rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #666666;
		background-repeat: no-repeat;
		background-position:left;
		background-size: 27rpx 27rpx;
		padding-left: 40rpx;
		float:right;
		height: 84rpx;
		line-height: 84rpx;
		margin-right: 29rpx;
	}
	.address_add{
		width: 750rpx;
		height: 104rpx;
		background: #FFFFFF;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
	}
	.address_add_a{
		width: 702rpx;
		height: 80rpx;
		background: linear-gradient(90deg, #18278f, #18278f);
		border-radius: 40rpx;
		margin: auto;
		margin-top: 13rpx;
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
		line-height: 80rpx;
		text-align: center;
	}
	.icon-lg {
	  min-height: 26px;
	  min-width: 26px;
	  height: 26px;
	  width: 26px;
	  vertical-align: middle;
	}
	.tips-dialog{
	  height: 115px;
	  width: 100%;
	}
</style>
